#++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
#### R-Script for Replication of "Soldiers and Protest ####

#~~~~~~~~~~~ International Interactionns (2023) ~~~~~~~~~~#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++#


# Author: Tanja Eschenauer-Engler
# Date: April 21, 2023
# R version 4.2.2

# The dataset file for replicating the data is "soldiers in protest_replication.csv
# The QCA was conducted using the packages QCA (Dusa 2019) and SetMethods (Oana and Schneider 2018).



##### Preparation ####
#~~~~~~~~~~~~~~~~~~~~~

# Starting commands (working space & directory)
rm(list=ls()) 

setwd("~/Desktop/Revision International Interactions")

# Loading packages & data

library(SetMethods)
library (QCA)

repression_protest <- read.csv("soldiers and protest_replication.csv", header = TRUE, row.names = 1, sep = ",", dec = ".")


##### Description of the dataset ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Outcome: repression (coding 0/1)

# Conditions (coding 0/1):

# P = Preferential recruitment
# U = military unity
# S = materially spoiled military
# V = prior engagement in domestic repression
# C = conscription
# Pers = personalist regime (Geddes, Wright, and Frantz 2014; only used in robustness check)



##### 1. Standard analysis: presence of outcome (military repression) ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# 1.1. Necessity analysis for military repression
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# Single necessary conditions (see table 2 in the main text)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QCAfit(x = repression_protest[,c("s", "u", "p", "v", "c")], 
       y = repression_protest$repression)

# result: spoils is a necessary condition for repression 


# SUIN conditions
#~~~~~~~~~~~~~~~~

SS_repression <- superSubset(data = repression_protest, 
                             outcome = "repression",
                             conditions = c("s", "u", "p", "v", "c"), 
                             incl.cut = 0.9,
                             cov.cut=0.6,
                             ron.cut=0.5 )
SS_repression

# disjunction V+~C has a consistency value of 0.909 and a RoN value of 0.643 
# however, not regarded as necessary because this union of conditions cannot be linked to a theoretically meaningful superordindate concept


# 1.2. Sufficiency analysis for military repression
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the truth table (see table A5 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TT_repression <- truthTable(data = repression_protest, 
                            outcome ="repression", 
                            conditions = c("s", "u", "p", "v", "c"), 
                            incl.cut = 0.8, 
                            pri.cut = 0.51,
                            n.cut = 1, 
                            sort.by = c("OUT", "incl", "n"),
                            show.cases = TRUE)
TT_repression


stargazerTT(TT_repression, show.cases = TRUE, type ="text", out = "TT_repression.txt")


# Logical minimization 
#~~~~~~~~~~~~~~~~~~~~~~

# Complex/Conservative solution (see table A6 in the appendix) 

sol_repression_complex <- minimize(TT_repression, 
                                   details = TRUE)

sol_repression_complex



# Most parsimonious solution (see tables A7 & A8 in the appendix)

sol_repression_pars <- minimize(TT_repression,
                                details = TRUE,
                                include = "?")
sol_repression_pars

# The results point to two models that explain the outcome (model ambguity). However, as the PI charts shows 
# there are contradictory rows included in the minimization process when contructing the parsimonious solution. 
# I therefore report in the enhanced parsimonious solution in the main text (see below).  


# Check simplifying assumptions

sol_repression_pars$SA

# row 8 and 16 in M2 contradict statement of necessity


# Intermediate solution (see table A9 in the appendix)

sol_repression_inter <- minimize(TT_repression, 
                                 details = TRUE, 
                                 include = "?",
                                 dir.exp = c(1, 1, 1, 1, 0))
sol_repression_inter

# directional expectations are based on the assumptions in the main text 

# Check easy counterfactuals

sol_repression_inter$i.sol$C1P1$EC



#### 2. Standard analysis: absence of outcome (~repression) ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 2.1. Necessity analysis for the absence of military repression
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# Necessary conditions (see table 4 in the main text) 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QCAfit(x = repression_protest[,c("s", "u", "p", "v", "c")], 
       y = 1-repression_protest$repression)

# result: none of the conditions is individually necessary for the absence of repression


# SUIN conditions
#~~~~~~~~~~~~~~~~
SS_norepression <- superSubset(data = repression_protest, 
                               outcome = "~repression",
                               conditions = c("s", "u", "p", "v", "c"), 
                               incl.cut = 0.9,
                               cov.cut=0.6,
                               ron.cut=0.6 )
SS_norepression

# result: four disjunctions pass the consistency threshold of 0.9 and have reasonable RoN scores: ~S + ~U, ~S + ~P,  ~S + ~C, ~P + ~V
# however, not regarded as necessary because these unions of conditions cannot be linked to a theoretically meaningful superordindate concept



# 2.2. Sufficiency analysis for the absence of military repression
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the truth table (see table A11 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


TT_norepression <- truthTable(data = repression_protest, 
                              outcome="~repression", 
                              conditions = c("s","p","u", "v", "c"), 
                              incl.cut = 0.8, 
                              pri.cut = 0.51,
                              n.cut = 1, 
                              sort.by = "incl",
                              show.cases = TRUE)

TT_norepression

stargazerTT(TT_norepression, show.cases = TRUE, type ="text", out = "TT_norepression.txt")


# Logical minimization 
#~~~~~~~~~~~~~~~~~~~~~~

# Complex/Conservative solution (see table A12 in the appendix)

sol_norepression_complex <- minimize(TT_norepression, 
                                     details = TRUE)

sol_norepression_complex



# Most parsimonious solution (see table 5 in the main text)
sol_norepression_pars <- minimize(TT_norepression,
                                  details = TRUE,
                                  include = "?")
sol_norepression_pars


# Show simplifying assumptions
sol_norepression_pars$SA


# Intermediate solution (see table A13 in the appendix)

sol_norepression_inter <- minimize(TT_norepression,
                                   details = TRUE,
                                   include = "?",
                                   dir.exp = c(0, 0, 0, 0, 1))

sol_norepression_inter


# Show easy counterfactuals

sol_norepression_inter$i.sol$C1P1$EC



#### 3. Enhanced standard analysis (ESA) for the presence of the outcome (military repression) ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# aim of this process: derive the enhanced parsimonious solution (integrated in the manuscript) and the enhanced intermediate solution (appendix)


# 3.1 Checks required to conduct ESA
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Are there simplifying assumptions that contradict the statement of necessity?
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# see 1.1: S (material spoils) was identified as a necessary condition for the presence of military repression in the necessity analysis


# Contradictory assumptions 
# ~~~~~~~~~~~~~~~~~~~~~~~~~

# for the enhanced most parsimonious solution

CSA <- intersect(rownames(sol_repression_pars$SA$M1), rownames(sol_norepression_pars$SA$M1))
CSA

# --> no contradictory simplifying assumptions 



CSAi <- intersect(rownames(sol_norepression_inter$i.sol$C1P1$EC), rownames(sol_repression_inter$i.sol$C1P1$EC))
CSAi

# none


# Simultaneous subset relations
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


SSR<-intersect(rownames(TT_norepression$tt)[TT_norepression$tt$OUT==1],rownames(TT_repression$tt)[TT_repression$tt$OUT==1])
SSR

# no simultaneous subset relations



### 3.2 Enhanced solutions
#~~~~~~~~~~~~~~~~~~~~~~~~~


# Constructing novel truth table for ESA
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TT_repression_esa <- esa(TT_repression,
                         nec_cond = "s")

TT_repression_esa 


# Enhanced most parsimonious solution (see table 3 in the main text)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sol_repression_pars_esa <- minimize(TT_repression_esa,
                                    details = TRUE,
                                    include = "?")

sol_repression_pars_esa


# Enhanced intermediate solution (see table A10 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sol_repression_inter_esa <- minimize(TT_repression_esa,
                                     details = TRUE,
                                     include = "?",
                                     dir.exp = c(1, 1, 1, 1, 0))

sol_repression_inter_esa



#### 4. Enhanced standard analysis for the absence of the outcome (~repression) ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 4.1 Checks required to conduct ESA
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

# Are there simplyfying assumptions that contradict the statement of necessity?
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# no, there are no contradictions 
# see 2.1: no condition is individually necessary for the outcome; no combination of conditions is declared necessary



# Contradictory assumptions 
# ~~~~~~~~~~~~~~~~~~~~~~~~~

# for the enhanced most parsimonious solution

CSA <- intersect(rownames(sol_norepression_pars$SA$M1), rownames(sol_repression_pars$SA$M1))
CSA


# --> no contradictory simplifying assumptions 


CSAi <- intersect(rownames(sol_norepression_inter$i.sol$C1P1$EC), rownames(sol_repression_inter$i.sol$C1P1$EC))
CSAi

# none


# Simultaneous subset relations
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


SSR<-intersect(rownames(TT_norepression$tt)[TT_norepression$tt$OUT==1],rownames(TT_repression$tt)[TT_repression$tt$OUT==1])
SSR

# no simultaneous subset relations


# Result of these checks
#~~~~~~~~~~~~~~~~~~~~~~~
# Since there are no contradictory assumptions and no simultaneous subset relations and none of the conditions (or combinations thereof) 
# is declared necessary, the results of the ESA (enhanced parsimonious and enhanced intermedia solution) are identical to the results of 
# the Standard Analysis. I therefore report the standard most parsimonious solution in the main text.



#### 5. Robustness test: adding indicator for personalist regimes ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# 5.1. Necessity analysis for military repression (with personalist regimes)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Single necessary conditions (table A 14)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QCAfit(x = repression_protest[,c("s", "u", "p", "v", "c", "pers")], 
       y = repression_protest$repression)

# personalist regimes (or its negation) is not a necessary conditions for military repression 
# results of all the other variables are - of course - identical to results from 1.1 and thus not reported


# SUIN conditions
# ~~~~~~~~~~~~~~~

SS_repression_pers <- superSubset(data = repression_protest, 
                                  outcome = "repression",
                                  conditions = c("s", "u", "p", "v", "c", "pers"), 
                                  incl.cut = 0.9,
                                  cov.cut=0.6,
                                  ron.cut=0.6 )
SS_repression_pers

# results are identical to 1.1
# disjunction V+~C has a consistency value of 0.909 and a RoN value of 0.643 
# however, not regarded as necessary because this union of conditions cannot be linked to a theoretically meaningful superordindate concept



# 5.2. Sufficiency analysis for military repression (with personalist regimes)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the truth table (not reported in the appendix, but can be displayed in R)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TT_repression_pers <- truthTable(data = repression_protest, 
                                 outcome ="repression", 
                                 conditions = c("s", "u", "p", "v", "c", "pers"), 
                                 incl.cut = 0.8, 
                                 pri.cut = 0.51,
                                 n.cut = 1, 
                                 sort.by = c("OUT", "incl", "n"),
                                 show.cases = TRUE)
TT_repression_pers


# Logical minimization 
# ~~~~~~~~~~~~~~~~~~~~

# I replicate the most parsimonious solution as I rely on the most parsimonious solution to causally interpret the results

# most parsimonious solution (see table A15 & A16 in the appendix)

sol_repression_pers_pars <- minimize(TT_repression_pers,
                                     details = TRUE,
                                     include = "?")
sol_repression_pers_pars


# most parsimonious solution of QCA with personalist regimes is identical to most parsimonious solution to QCA without personalist regimes




# 5.3. Necessity analysis for the absence of military repression (~repression )with personalist regimes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Single necessary coditions (see table A17 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QCAfit(x = repression_protest[,c("s", "u", "p", "v", "c", "pers")], 
       y = 1-repression_protest$repression)

# the condition personalist regime (as well as its negation) is not individually necessary for the absence of military repression 


# SUIN conditions
#~~~~~~~~~~~~~~~~

SS_norepression_pers <- superSubset(data = repression_protest, 
                                    outcome = "~repression",
                                    conditions = c("s", "u", "p", "v", "c", "pers"), 
                                    incl.cut = 0.9,
                                    cov.cut=0.6,
                                    ron.cut=0.6 )
SS_norepression_pers


# results are identical to 2.1 
# result: four disjunctions pass the consistency threshold of 0.9 and have reasonable RoN scores: ~S + ~U, ~S + ~P,  ~S + ~C, ~P + ~V
# however, not regarded as necessary because these unions of conditions cannot be linked to a theoretically meaningful superordindate concept



# 5.4. Sufficiency analysis for the absence of military repression (with personalist regimes)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the truth table (not reported in the appendix, but can be displayed in R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TT_norepression_pers <- truthTable(data = repression_protest, 
                                   outcome="~repression", 
                                   conditions = c("s","p","u", "v", "c", "pers"), 
                                   incl.cut = 0.8, 
                                   pri.cut = 0.51,
                                   n.cut = 1, 
                                   sort.by = "incl",
                                   show.cases = TRUE)

TT_norepression_pers

# Logical minimization
#~~~~~~~~~~~~~~~~~~~~~

# I replicate the most parsimonious solution as I report the most parsimonious solution for the absence of military repression
# in the main paper. 


# Parsimonious solution (see table A18 in the appendix)
sol_norepression_pers_pars <- minimize(TT_norepression_pers,
                                       details = TRUE,
                                       include = "?")
sol_norepression_pers_pars

# results are identical to the most parsimonious solution reported in the main text (see table table 5 in the main text)



# 5.5. Enhanced standard analysis for outcome military repression when including condition personalist regimes 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# since the most parsimonious solution from ESA is reported in the main text, I also obtain the most parsimonious solution when condition 
# personalist regimes is included in the sufficiency analysis for the outcome military repression. 


# Are there simplyfying Assumptions that contradict the statement of necessity?
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# see 5.1: S (material spoils) was identified as a necessary condition for military repression in the necessity analysis


# Contradictory assumptions 
#~~~~~~~~~~~~~~~~~~~~~~~~~~

# for the enhanced most parsimonious solution

CSA <- intersect(rownames(sol_norepression_pers_pars$SA$M1), rownames(sol_repression_pers_pars$SA$M1))
CSA

LR.intersect(sol_norepression_pers_pars, sol_repression_pers_pars)


# --> no contradictory simplifying assumptions 


# Simultaneous subset relations
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


SSR<-intersect(rownames(TT_norepression_pers$tt)[TT_norepression_pers$tt$OUT==1],rownames(TT_repression_pers$tt)[TT_repression_pers$tt$OUT==1])
SSR

# no simultaneous subset relations


# Obtaining the enhanced most parsimonious solution 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing a novel truth table for ESA (not reported in the appendix, but can be displayed in R)

TT_repression_pers_esa <- esa(TT_repression_pers,
                              nec_cond = "s")

TT_repression_pers_esa 



# Obtaining the enhanced parsimonious solution (see table A19 in the appendix)


sol_repression_pers_pars_esa <- minimize(TT_repression_pers_esa,
                                         details = TRUE,
                                         include = "?")

sol_repression_pers_pars_esa

# enhanced most parsimonious solution for military repression when integrating personalist regimes 
# is identical to the solution in the main text without personalist regime indicator



# 5.6. Enhanced Standard Analysis for absence of military repression when including condition personalist regimes 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Since there are not simultaneous subset relations, contradictory assumptions, and necessary conditions, the 
# enhanced most parsimonious solution is identical to the most parsimonious solution (see 5.4). 


# end of script







